define(function (require, exports, module) {
    var $ = require("jquery");
    require("jqtree");
    var angular = require("angular");

    angular
        .module('teamAdd', [])
        .directive("uiTeamAdd", [
			'$compile',
			function ($compile) {
            return {
                //require: "ngModel",
                restrict: "AE",
                scope: {
                    "teamInfo": "=ngModel",
					"user": "=data"
                },
                link: function (scope, el, attrs) {    
					var contentTpl = [
						'<div class="join_team_dialog" id="addTeamMenu">',
							'<div class="join_team_dialog_select">',
								'<i class="fag"></i>',
								'<p>',
									'<span>用户身份：</span>',
									'<a href="javascript:;" class="{{curRole.name === role.name ? \'selected\' : \'\'}}" ng-repeat="role in roles" ng-click="switchRole($index)">{{role.name}}</a>',
								'</p>',
								'<div class="mt10 clearfix">',
									'<span class="fl">所属团队：</span>',
									'<div class="select fl ml5" style="width: 280px;">',
										'<span class="select-border js-select2" ng-click="toggleTree()">',
											'<input class="select-text " type="text" readonly value="{{selected.group_name || selected.team_name}}"/>',
											'<span class="select-icon">',
												'<i class="select-icon-arrow"></i>',
											'</span>',
										'</span>',
										'<div class="select-sub" style="border:none;">',
											'<div id="teamTree"></div>',
										'</div>',
									'</div>',
								'</div>',
								'<p></p>',
								'<p class="clearfix mt15">',
									'<span class="fr">',
										'<a href="javascript:;" class="btn" ng-click="ok()" style="color:#fff;">确定</a>',
										'<a href="javascript:;" class="btn btn-gray" ng-click="cancel()">取消</a>',
									'</span>',
								'</p>',
							'</div>',
						'</div>'
					].join('');      
					
					scope.roles = [{
						name: "置业顾问",
						type: "1"
					},{
						name: "销售经理",
						type: "2"
					},{
						name: "团队经理",
						type: "3"
					},{
						name: "销售组长",
						type: "4"
					}];
					
					scope.curRole = scope.roles[0];
					
					scope.switchRole = function(index) {
						scope.curRole = scope.roles[index];
						if (scope.curRole.name === "销售经理") {
							$('#teamTree').parent().parent().parent().hide();
						} else {
							$('#teamTree').parent().parent().parent().show();
						}
						scope.selected = null;
						scope.$apply();
					};
					
					scope.cancel = function(){
						removeMenu();
					};
					
					scope.ok = function(){
						scope.$parent.addTeamUser(scope.user,scope.curRole.type,scope.selected)
							.then(function(){
								removeMenu();
							})
					};
					
					function removeMenu() {
						var menu = $('#addTeamMenu');
						
						if (menu.length) {
							menu.remove();
						}
					}
					
					function showMenu () {
						removeMenu();
						
						var menu = $compile(contentTpl)(scope)
						$(el).after(menu);
						
						showTree(scope.teamInfo)
					}
					
                    $(el).on('click',function(){
						scope.$apply(function(){
							showMenu();
						})
					});
					
					scope.toggleTree = function(){
						$('#teamTree').parent().toggle();
					}
					
					function showTree(teamInfo) {
						var teams = teamInfo.teams;
						var teamList = [];
						for (var i = 0; i < teams.length; i++) {
							var team = teams[i];
							if (!team.team_id) {
								break;
							}
							teamList[i] = {};
							teamList[i].name = team.team_name;
							teamList[i].data = [i];

							if (team.groups && team.groups.length && team.groups[0].group_name) {
								teamList[i].children = [];
								for (var j = 0; j < team.groups.length; j++) {
									teamList[i].children.push({
										name: team.groups[j].group_name,
										data: [i, j]
									});
								}
							}

						}
						
						if (!teamList.length) {
							scope.showTree = false;
							return;
						}
						
						var $tree = $('#teamTree');
						
						$tree.tree("destroy");
						$tree.tree({
							data: teamList,
							dragAndDrop: false,
							useContextMenu: false,
							autoOpen: 0, //展开级别
							onCreateLi: function (node, $li) {
							},
							//是否可以移动
							onCanMove: function (node) {
								return false;
							},
							//是否可以移动到
							onCanMoveTo: function (moved_node, target_node, position) {
								//只能在同级节点中移动
								return false;
							},
							//移动句柄
							onIsMoveHandle: function ($ele) {
								return false;
							}
						})
						.on('tree.click', function (event) {
							if (event.node) {
								var data = event.node.data;
								if (data.length === 1) {

									if (scope.curRole.name == "置业顾问"){
										var team = teams[data[0]];
										if (!(team.groups && team.groups[0] && team.groups[0].group_id)) {
											scope.selected = teams[data[0]];
										}
									} else if (scope.curRole.name === "销售组长") {
										
									} else {
										scope.selected = teams[data[0]];
									}
									
								} else {
									if (scope.curRole.name === "置业顾问" || scope.curRole.name === "销售组长") {
										scope.selected = teams[data[0]].groups[data[1]];
									}                                       
								}
								scope.toggleTree();
								scope.$apply();
							}
						});
					}
					
					
                }
            };
                 
        }])
});